package goodbaby.dklt.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.classic.core.activity.BaseActivity;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;
import com.tgd.gbb.uikit.ui.refresh.PullToRefreshScrollView;
import com.tgd.gbb.uikit.ui.refresh.base.PullToRefreshBase;
import com.tgd.gbb.uikit.ui.widget.MyListView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.adapter.CommonAdapter;
import goodbaby.dklt.adapter.ViewHolder;
import goodbaby.dklt.bean.EducNoticInfo;
import goodbaby.dklt.bean.MessageInfo;

/**
 * Created by Li.z on 2016/5/7.
 * 教务公告
 */
public class EducNoticeActivity extends BaseActivity {

    public static final String TAG = "EducNoticeActivity";

    private TextView tvTitle;
    private ImageView ivBack;

    private MyListView listView;

    private PullToRefreshScrollView mPullRefreshScrollView;

    private int pageNum = 1;

    private MessageAdapter mAdapter;

    @Override
    public int getLayoutResId() {
        return R.layout.activity_educ_notice;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void initView() {
        super.initView();
        tvTitle = (TextView) findViewById(R.id.tv_common_title);
        tvTitle.setText("教务公告");
        ivBack = (ImageView) findViewById(R.id.iv_common_back);
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(this);

        listView = (MyListView) findViewById(R.id.column_detail_listview);
        mPullRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_scrollview);

        //上拉、下拉设定
        mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);

        mPullRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {

                if (mPullRefreshScrollView.isHeaderShown()) {
                    //下拉刷新
                    toRefresh();
                } else if (mPullRefreshScrollView.isFooterShown()) {
                    //上拉加载
                    toLoadMore();
                }

            }
        });


        mAdapter = new MessageAdapter(activity, null);
        listView.setAdapter(mAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        listView.postDelayed(new Runnable() {
            @Override
            public void run() {
                loadData();
            }
        }, 600);
    }

    private void loadData() {
        mPullRefreshScrollView.setRefreshing();
    }


    @Override
    public void initData() {
        super.initData();
    }

    @Override
    public void viewClick(View v) {
        super.viewClick(v);
        switch (v.getId()) {
            case R.id.iv_common_back:
                finish();
                break;
        }
    }


    private void toRefresh() {
        pageNum = 1;
        ActionHelp.getEducNoticeInfo(activity, pageNum, new ObjectCallback<ArrayList<EducNoticInfo>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(EducNoticeActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
                Log.i(TAG, "请求错误" + code + ";" + code);
                mPullRefreshScrollView.onRefreshComplete();
            }

            @Override
            public void onSuccess(ArrayList<EducNoticInfo> response) {
                Log.i(TAG, response.toString());
                pageNum++;
                mPullRefreshScrollView.onRefreshComplete();
                mAdapter.setData(response);
                mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
            }

            @Override
            public Type getType() {
                return new TypeToken<ArrayList<EducNoticInfo>>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
                mPullRefreshScrollView.onRefreshComplete();
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
                mPullRefreshScrollView.onRefreshComplete();
            }
        });
    }

    private void toLoadMore() {
        ActionHelp.getEducNoticeInfo(activity, pageNum, new ObjectCallback<ArrayList<EducNoticInfo>>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(EducNoticeActivity.this, LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onSuccess(ArrayList<EducNoticInfo> response) {
                Log.i(TAG, response.toString());
                pageNum++;
                mAdapter.addData(response);
                mPullRefreshScrollView.onRefreshComplete();
                if (response != null && response.size() < 10) {
                    mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
                } else {
                    mPullRefreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<ArrayList<EducNoticInfo>>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                Log.i(TAG, "请求错误" + code + ";" + errorMessage);
                ToastUtil.showToast(activity, "请求错误" + code);
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(activity, "请求超时");
            }
        });
    }

    public class MessageAdapter extends CommonAdapter<EducNoticInfo> {


        public MessageAdapter(Context context, List<EducNoticInfo> mDatas) {
            super(context, R.layout.item_list_message, mDatas);
        }

        @Override
        public void convert(ViewHolder helper, EducNoticInfo item, int position) {
            ImageView ivRead = helper.getView(R.id.iv_msg_read);
            TextView tvTitle = helper.getView(R.id.tv_title);
            TextView tvContent = helper.getView(R.id.tv_content);
            TextView tvDate = helper.getView(R.id.tv_date);

            ivRead.setEnabled(false);

            tvTitle.setText("教务公告");

            tvContent.setText(item.getCONTENT());

            tvDate.setText(item.getTeacherName() + "   " + item.getSEND_DATE().substring(0, 10));

        }
    }
}
